<?php
class Notif extends Model {
	function Notif(){
		parent::Model();
    }
    
	function insertNotifMentioned($post_id=NULL,$user_id=NULL,$mentioner_id=NULL){
   		$query = "INSERT INTO notif_post_mentioned (post_id,user_id,mentioner_id,finish) VALUES ('$post_id','$user_id','$mentioner_id','0')";
   		if($query = $this->db->query($query)){
    		return $this->db->insert_id();
    	}
    	return FALSE;
    }
    
    function getAllNotifMentionedUnread($user_id=NULL){
    	$return = FALSE;
    	$query = "SELECT * FROM notif_post_mentioned WHERE user_id='$user_id' AND finish=0 ORDER BY id DESC";
    	$query = $this->db->query($query);
   	 	if($query->num_rows() > 0){
   	 		foreach ($query->result() as $row){
   	 			$return['id'][]  = $row->id;
				$return['user_id'][]  = $row->user_id;
				$return['post_id'][] = $row->post_id;
				$return['mentioner_id'][] = $row->mentioner_id;
				$return['finish'][] = $row->finish;
			}
    	}
    	return $return;
    }
    
	function getAllNotifMentionedRead($user_id=NULL){
    	$return = FALSE;
    	$query = "SELECT * FROM notif_post_mentioned WHERE user_id='$user_id' AND finish=1 ORDER BY id DESC";
    	$query = $this->db->query($query);
   	 	if($query->num_rows() > 0){
   	 		foreach ($query->result() as $row){
				$return['user_id'][]  = $row->user_id;
				$return['post_id'][] = $row->post_id;
				$return['mentioner_id'][] = $row->mentioner_id;
				$return['finish'][] = $row->finish;
			}
    	}
    	return $return;
    }
    function setFinishMentioned($mentioned_id){
    	$query = "UPDATE notif_post_mentioned SET finish=1 WHERE id='$mentioned_id'";
    	$query = $this->db->query($query);
    }
    function setFinishCommented($commented_id){
    	$query = "UPDATE notif_post_comment SET finish=1 WHERE id='$commented_id'";
    	$query = $this->db->query($query);
    }
    function addCommented($user_id=NULL,$post_id=NULL,$efect_id=NULL){
    	$query = "INSERT INTO notif_post_comment (post_id,user_id,comentator_id,finish) VALUES ('$post_id','$efect_id','$user_id','0')";
   		return $this->db->query($query);
    		 
    }
    function getAllNotifCommented($user_id=NULL){
    	$return = FALSE;
    	$query = "SELECT * FROM notif_post_comment WHERE user_id='$user_id' ORDER BY finish ASC,id DESC";
    	$query = $this->db->query($query);
   	 	if($query->num_rows() > 0){
   	 		foreach ($query->result() as $row){
   	 			$return['id'][]  = $row->id;
				$return['user_id'][]  = $row->user_id;
				$return['post_id'][] = $row->post_id;
				$return['commenter_id'][] = $row->comentator_id;
				$return['finish'][] = $row->finish;
			}
    	}
    	return $return;
    }
    
    function getAllNotifFriend($user_id=NULL){
    	$return = FALSE;
    	$query = "SELECT * FROM notif_req_friend WHERE user_id='$user_id' AND finish='0' ORDER BY id DESC";
    	$query = $this->db->query($query);
   	 	if($query->num_rows() > 0){
   	 		foreach ($query->result() as $row){
   	 			$return['id'][]  = $row->id;
				$return['user_id'][]  = $row->user_id;
				$return['friend_id'][] = $row->friend_id;
				$return['finish'][] = $row->finish;
			}
    	}
    	return $return;
    }
    
    function setIgnored($req_id){
    	$query = "DELETE FROM notif_req_friend WHERE id='$req_id'";
    	$query = $this->db->query($query);
    }
    function setConfirm($req_id){
    	$query = "UPDATE notif_req_friend SET finish=1 WHERE id='$req_id'";
    	$query = $this->db->query($query);
    }
}